﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.AlterManagement;
using Bricks.Business.Models.ProjectContract;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Site.Models.DBViewModel.Contract;
using Bricks.Component.Data;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IAlterService))]
    internal class AlterService : IAlterService
    {
        #region 属性
        [Import]
        protected IAlterRepository AlterRepository { get; set; }
        [Import]
        protected IAlterBackupRepository AlterBackupRepository { get; set; }
        [Import]
        protected IAlterBillRepository AlterBillRepository { get; set; }
        [Import]
        protected IAlterBillBackupRepository AlterBillBackupRepository { get; set; }
        [Import]
        protected IProjectInfoRepository ProjectInfoRepository { get; set; }
        //[Import]
        //protected IContractInfoRepository ContractInfoRepository { get; set; }
        public IQueryable<AlterBackup> AlterBackups
        {
            get { return AlterBackupRepository.Entities; }
        }
        #endregion

        #region 获取单据和清单数据
        public AlterView GetAlterByID(string alterID)
        {
            Guid queryID = Guid.Parse(alterID);
            AlterView alterView = null;
            Alter alter = AlterRepository.Entities.FirstOrDefault(a => a.ID == queryID);
            if (alter != null)
            {
                alterView = new AlterView();
                alterView.ID = alter.ID;
                alterView.AlterID = alter.ID;
                alterView.FlowCode = alter.FlowCode;
                alterView.ProjectID = alter.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alter.ProjectID);
                if (projectInfo != null)
                {
                    alterView.ProjectName = projectInfo.ProjectName;
                }
                alterView.ContractID = alter.ContractID;
                alterView.AlterCode = alter.AlterCode;
                alterView.AlterName = alter.AlterName;
                alterView.AlterPosition = alter.AlterPosition;
                alterView.AlterType = alter.AlterType;
                alterView.OriginalChartName = alter.OriginalChartName;
                alterView.AlterChartName = alter.AlterChartName;
                alterView.OriginalChartNo = alter.OriginalChartNo;
                alterView.AlterChartNo = alter.AlterChartNo;
                alterView.AlterReason = alter.AlterReason;
                alterView.AlterMoney = alter.AlterMoney;
                alterView.AlterDate = alter.AlterDate;
                alterView.RequestPerson = alter.RequestPerson;
                alterView.RequestDate = alter.RequestDate;
                alterView.ProcedureMonitor = alter.ProcedureMonitor;
                alterView.HandleState = alter.HandleState;
                alterView.AssociatedNodeCode = "";
                alterView.LastUpdateUserDepartment = alter.LastUpdateUserDepartment;
                alterView.LastUpdateUser = alter.LastUpdateUser;
                alterView.LastUpdateTime = alter.LastUpdateTime;
                //alterView.SourceNodeCode = "";
                //alterView.SourceUserDepartment = "";
                //alterView.SourceUser = "";
                alterView.SaveState = "0";
                alterView.SendState = "1";
            }
            else
            {
                AlterBackup alterBackup = AlterBackupRepository.Entities.FirstOrDefault(a => a.AlterID == queryID);
                if (alterBackup != null)
                {
                    alterView = new AlterView();
                    alterView.ID = alterBackup.ID;
                    alterView.AlterID = alterBackup.AlterID;
                    alterView.FlowCode = alterBackup.FlowCode;
                    alterView.ProjectID = alterBackup.ProjectID;
                    ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterBackup.ProjectID);
                    if (projectInfo != null)
                    {
                        alterView.ProjectName = projectInfo.ProjectName;
                    }
                    alterView.ContractID = alterBackup.ContractID;
                    alterView.AlterCode = alterBackup.AlterCode;
                    alterView.AlterName = alterBackup.AlterName;
                    alterView.AlterPosition = alterBackup.AlterPosition;
                    alterView.AlterType = alterBackup.AlterType;
                    alterView.OriginalChartName = alterBackup.OriginalChartName;
                    alterView.AlterChartName = alterBackup.AlterChartName;
                    alterView.OriginalChartNo = alterBackup.OriginalChartNo;
                    alterView.AlterChartNo = alterBackup.AlterChartNo;
                    alterView.AlterReason = alterBackup.AlterReason;
                    alterView.AlterMoney = alterBackup.AlterMoney;
                    alterView.AlterDate = alterBackup.AlterDate;
                    alterView.RequestPerson = alterBackup.RequestPerson;
                    alterView.RequestDate = alterBackup.RequestDate;
                    alterView.ProcedureMonitor = alterBackup.ProcedureMonitor;
                    alterView.HandleState = alterBackup.HandleState;
                    alterView.AssociatedNodeCode = alterBackup.AssociatedNodeCode;
                    alterView.LastUpdateUserDepartment = alterBackup.LastUpdateUserDepartment;
                    alterView.LastUpdateUser = alterBackup.LastUpdateUser;
                    alterView.LastUpdateTime = alterBackup.LastUpdateTime;
                    //alterView.SourceNodeCode = alterBackup.SourceNodeCode;
                    //alterView.SourceUserDepartment = alterBackup.SourceUserDepartment;
                    //alterView.SourceUser = alterBackup.SourceUser;
                    alterView.SaveState = alterBackup.SaveState;
                    alterView.SendState = alterBackup.SendState;
                }
            }
            return alterView;
        }

        public List<AlterBillView> GetAlterBillList(Guid alterID)
        {
            IQueryable<AlterBill> alterBills = AlterBillRepository.Entities.Where(a => a.PID == alterID);
            List<AlterBillView> alterBillViewList = new List<AlterBillView>();
            foreach (AlterBill alterBill in alterBills)
            {
                alterBillViewList.Add(new AlterBillView()
                {
                    ID = GuidHelper.NewGuid(),
                    PID = alterBill.PID,
                    AlterBillID = alterBill.ID,
                    BillCode = alterBill.BillCode,
                    BillName = alterBill.BillName,
                    Unit = alterBill.Unit,
                    OriginalCount = alterBill.OriginalCount,
                    AfterAlterCount = alterBill.AfterAlterCount,
                    AlterCount = alterBill.AlterCount,
                    OriginalPrice = alterBill.OriginalPrice,
                    PresentPrice = alterBill.PresentPrice,
                    OriginalMoney = alterBill.OriginalMoney,
                    PresentMoney = alterBill.PresentMoney,
                    AlterMoney = alterBill.AlterMoney,
                    Remark = alterBill.Remark,
                    AssociatedNodeCode = "",
                    LastUpdateUserDepartment = alterBill.LastUpdateUserDepartment,
                    LastUpdateUser = alterBill.LastUpdateUser,
                    LastUpdateTime = alterBill.LastUpdateTime,
                    SendState = "0"
                });
            }
            return alterBillViewList;
        }
        #endregion

        #region 获取单据和清单的过程数据
        public AlterView GetAlterBackupByID(Guid id)
        {
            AlterView alterView = null;
            AlterBackup alterBackup = AlterBackupRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (alterBackup != null)
            {
                alterView = new AlterView();
                alterView.ID = alterBackup.ID;
                alterView.AlterID = alterBackup.AlterID;
                alterView.FlowCode = alterBackup.FlowCode;
                alterView.ProjectID = alterBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterView.ProjectName = projectInfo.ProjectName;
                }
                alterView.ContractID = alterBackup.ContractID;
                alterView.AlterCode = alterBackup.AlterCode;
                alterView.AlterName = alterBackup.AlterName;
                alterView.AlterPosition = alterBackup.AlterPosition;
                alterView.AlterType = alterBackup.AlterType;
                alterView.OriginalChartName = alterBackup.OriginalChartName;
                alterView.AlterChartName = alterBackup.AlterChartName;
                alterView.OriginalChartNo = alterBackup.OriginalChartNo;
                alterView.AlterChartNo = alterBackup.AlterChartNo;
                alterView.AlterReason = alterBackup.AlterReason;
                alterView.AlterMoney = alterBackup.AlterMoney;
                alterView.AlterDate = alterBackup.AlterDate;
                alterView.RequestPerson = alterBackup.RequestPerson;
                alterView.RequestDate = alterBackup.RequestDate;
                alterView.ProcedureMonitor = alterBackup.ProcedureMonitor;
                alterView.HandleState = alterBackup.HandleState;
                alterView.AssociatedNodeCode = alterBackup.AssociatedNodeCode;
                alterView.LastUpdateUserDepartment = alterBackup.LastUpdateUserDepartment;
                alterView.LastUpdateUser = alterBackup.LastUpdateUser;
                alterView.LastUpdateTime = alterBackup.LastUpdateTime;
                //alterView.SourceNodeCode = alterBackup.SourceNodeCode;
                //alterView.SourceUserDepartment = alterBackup.SourceUserDepartment;
                //alterView.SourceUser = alterBackup.SourceUser;
                alterView.SaveState = alterBackup.SaveState;
                alterView.SendState = alterBackup.SendState;
            }
            return alterView;
        }

        public AlterView GetAlterBackupByAlterID(string alterID)
        {
            Guid queryID = Guid.Parse(alterID);
            AlterView alterView = null;
            AlterBackup alterBackup = AlterBackupRepository.Entities.Where(a => a.AlterID == queryID && a.SendState == "1").OrderByDescending(a => a.LastUpdateTime).FirstOrDefault();
            if (alterBackup != null)
            {
                alterView = new AlterView();
                alterView.ID = alterBackup.ID;
                alterView.AlterID = alterBackup.AlterID;
                alterView.FlowCode = alterBackup.FlowCode;
                alterView.ProjectID = alterBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterView.ProjectName = projectInfo.ProjectName;
                }
                alterView.ContractID = alterBackup.ContractID;
                alterView.AlterCode = alterBackup.AlterCode;
                alterView.AlterName = alterBackup.AlterName;
                alterView.AlterPosition = alterBackup.AlterPosition;
                alterView.AlterType = alterBackup.AlterType;
                alterView.OriginalChartName = alterBackup.OriginalChartName;
                alterView.AlterChartName = alterBackup.AlterChartName;
                alterView.OriginalChartNo = alterBackup.OriginalChartNo;
                alterView.AlterChartNo = alterBackup.AlterChartNo;
                alterView.AlterReason = alterBackup.AlterReason;
                alterView.AlterMoney = alterBackup.AlterMoney;
                alterView.AlterDate = alterBackup.AlterDate;
                alterView.RequestPerson = alterBackup.RequestPerson;
                alterView.RequestDate = alterBackup.RequestDate;
                alterView.ProcedureMonitor = alterBackup.ProcedureMonitor;
                alterView.HandleState = alterBackup.HandleState;
                alterView.AssociatedNodeCode = alterBackup.AssociatedNodeCode;
                alterView.LastUpdateUserDepartment = alterBackup.LastUpdateUserDepartment;
                alterView.LastUpdateUser = alterBackup.LastUpdateUser;
                alterView.LastUpdateTime = alterBackup.LastUpdateTime;
                //alterView.SourceNodeCode = alterBackup.SourceNodeCode;
                //alterView.SourceUserDepartment = alterBackup.SourceUserDepartment;
                //alterView.SourceUser = alterBackup.SourceUser;
                alterView.SaveState = alterBackup.SaveState;
                alterView.SendState = alterBackup.SendState;
            }
            return alterView;
        }

        public AlterView GetAlterBackupByCondition(string alterID, string nodeCode, string department, string userName)
        {
            Guid queryID = Guid.Parse(alterID);
            AlterView alterView = null;
            AlterBackup alterBackup = AlterBackupRepository.Entities.FirstOrDefault(a => a.AlterID == queryID && a.AssociatedNodeCode == nodeCode && a.LastUpdateUserDepartment == department && a.LastUpdateUser == userName);
            if (alterBackup != null)
            {
                alterView = new AlterView();
                alterView.ID = alterBackup.ID;
                alterView.AlterID = alterBackup.AlterID;
                alterView.FlowCode = alterBackup.FlowCode;
                alterView.ProjectID = alterBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterView.ProjectName = projectInfo.ProjectName;
                }
                alterView.ContractID = alterBackup.ContractID;
                alterView.AlterCode = alterBackup.AlterCode;
                alterView.AlterName = alterBackup.AlterName;
                alterView.AlterPosition = alterBackup.AlterPosition;
                alterView.AlterType = alterBackup.AlterType;
                alterView.OriginalChartName = alterBackup.OriginalChartName;
                alterView.AlterChartName = alterBackup.AlterChartName;
                alterView.OriginalChartNo = alterBackup.OriginalChartNo;
                alterView.AlterChartNo = alterBackup.AlterChartNo;
                alterView.AlterReason = alterBackup.AlterReason;
                alterView.AlterMoney = alterBackup.AlterMoney;
                alterView.AlterDate = alterBackup.AlterDate;
                alterView.RequestPerson = alterBackup.RequestPerson;
                alterView.RequestDate = alterBackup.RequestDate;
                alterView.ProcedureMonitor = alterBackup.ProcedureMonitor;
                alterView.HandleState = alterBackup.HandleState;
                alterView.AssociatedNodeCode = alterBackup.AssociatedNodeCode;
                alterView.LastUpdateUserDepartment = alterBackup.LastUpdateUserDepartment;
                alterView.LastUpdateUser = alterBackup.LastUpdateUser;
                alterView.LastUpdateTime = alterBackup.LastUpdateTime;
                //alterView.SourceNodeCode = alterBackup.SourceNodeCode;
                //alterView.SourceUserDepartment = alterBackup.SourceUserDepartment;
                //alterView.SourceUser = alterBackup.SourceUser;
                alterView.SaveState = alterBackup.SaveState;
                alterView.SendState = alterBackup.SendState;
            }
            return alterView;
        }

        public List<AlterBillView> GetAlterBillBackupList(Guid alterID, string nodeCode, string department, string userName)
        {
            IQueryable<AlterBillBackup> alterBillBackups = AlterBillBackupRepository.Entities.Where(a => a.PID == alterID && a.AssociatedNodeCode == nodeCode && a.LastUpdateUserDepartment == department && a.LastUpdateUser == userName);
            List<AlterBillView> alterBillViewList = new List<AlterBillView>();
            foreach (AlterBillBackup alterBillBackup in alterBillBackups)
            {
                alterBillViewList.Add(new AlterBillView()
                {
                    ID = alterBillBackup.ID,
                    PID = alterBillBackup.PID,
                    AlterBillID = alterBillBackup.AlterBillID,
                    BillCode = alterBillBackup.BillCode,
                    BillName = alterBillBackup.BillName,
                    Unit = alterBillBackup.Unit,
                    OriginalCount = alterBillBackup.OriginalCount,
                    AfterAlterCount = alterBillBackup.AfterAlterCount,
                    AlterCount = alterBillBackup.AlterCount,
                    OriginalPrice = alterBillBackup.OriginalPrice,
                    PresentPrice = alterBillBackup.PresentPrice,
                    OriginalMoney = alterBillBackup.OriginalMoney,
                    PresentMoney = alterBillBackup.PresentMoney,
                    AlterMoney = alterBillBackup.AlterMoney,
                    Remark = alterBillBackup.Remark,
                    AssociatedNodeCode = alterBillBackup.AssociatedNodeCode,
                    LastUpdateUserDepartment = alterBillBackup.LastUpdateUserDepartment,
                    LastUpdateUser = alterBillBackup.LastUpdateUser,
                    LastUpdateTime = alterBillBackup.LastUpdateTime,
                    SendState = alterBillBackup.SendState
                });
            }
            return alterBillViewList;
        }
        #endregion

        #region 保存单据和清单数据
        public void SaveAlter(AlterView alterView, List<AlterBillView> saveList)
        {
            Alter record = AlterRepository.Entities.FirstOrDefault(a => a.ID == alterView.AlterID);
            if (record != null)
            {
                UpdateAlter(record, alterView);
            }
            else
            {
                InsertAlter(alterView);
            }

            DeleteAlterBill(alterView);
            foreach (AlterBillView alterBillView in saveList)
            {
                InsertAlterBill(alterBillView);
            }
        }

        private void InsertAlter(AlterView alterView)
        {
            Alter record = new Alter()
            {
                ID = alterView.AlterID,
                FlowCode = alterView.FlowCode,
                ProjectID = alterView.ProjectID,
                ContractID = alterView.ContractID,
                AlterCode = alterView.AlterCode,
                AlterName = alterView.AlterName,
                AlterPosition = alterView.AlterPosition,
                AlterType = alterView.AlterType,
                OriginalChartName = alterView.OriginalChartName,
                AlterChartName = alterView.AlterChartName,
                OriginalChartNo = alterView.OriginalChartNo,
                AlterChartNo = alterView.AlterChartNo,
                AlterReason = alterView.AlterReason,
                AlterMoney = alterView.AlterMoney,
                AlterDate = alterView.AlterDate,
                RequestPerson = alterView.RequestPerson,
                RequestDate = alterView.RequestDate,
                ProcedureMonitor = alterView.ProcedureMonitor,
                HandleState = alterView.HandleState,
                LastUpdateUserDepartment = alterView.LastUpdateUserDepartment,
                LastUpdateUser = alterView.LastUpdateUser,
                LastUpdateTime = alterView.LastUpdateTime
            };
            AlterRepository.Insert(record);
        }

        private void UpdateAlter(Alter record, AlterView alterView)
        {
            record.ProjectID = alterView.ProjectID;
            record.ContractID = alterView.ContractID;
            record.AlterCode = alterView.AlterCode;
            record.AlterName = alterView.AlterName;
            record.AlterPosition = alterView.AlterPosition;
            record.AlterType = alterView.AlterType;
            record.OriginalChartName = alterView.OriginalChartName;
            record.AlterChartName = alterView.AlterChartName;
            record.OriginalChartNo = alterView.OriginalChartNo;
            record.AlterChartNo = alterView.AlterChartNo;
            record.AlterReason = alterView.AlterReason;
            record.AlterMoney = alterView.AlterMoney;
            record.AlterDate = alterView.AlterDate;
            record.ProcedureMonitor = alterView.ProcedureMonitor;
            record.HandleState = alterView.HandleState;
            if (string.IsNullOrEmpty(alterView.LastUpdateUserDepartment))
            {
                record.LastUpdateUserDepartment = alterView.LastUpdateUserDepartment;
            }
            if (string.IsNullOrEmpty(alterView.LastUpdateUser))
            {
                record.LastUpdateUser = alterView.LastUpdateUser;
            }
            record.LastUpdateTime = alterView.LastUpdateTime;
            AlterRepository.Update(record);
        }

        private void DeleteAlterBill(AlterView alterView)
        {
            IQueryable<AlterBill> lstAlterBill = AlterBillRepository.Entities.Where(a => a.PID == alterView.AlterID);
            List<Guid> lstDeleteID = new List<Guid>();
            foreach (AlterBill alterBill in lstAlterBill)
            {
                lstDeleteID.Add(alterBill.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                AlterBillRepository.Delete(id);
            }
        }

        private void InsertAlterBill(AlterBillView alterBillView)
        {
            AlterBill record = new AlterBill()
            {
                ID = alterBillView.AlterBillID,
                PID = alterBillView.PID,
                BillCode = alterBillView.BillCode,
                BillName = alterBillView.BillName,
                Unit = alterBillView.Unit,
                OriginalCount = alterBillView.OriginalCount,
                AfterAlterCount = alterBillView.AfterAlterCount,
                AlterCount = alterBillView.AlterCount,
                OriginalPrice = alterBillView.OriginalPrice,
                PresentPrice = alterBillView.PresentPrice,
                OriginalMoney = alterBillView.OriginalMoney,
                PresentMoney = alterBillView.PresentMoney,
                AlterMoney = alterBillView.AlterMoney,
                Remark = alterBillView.Remark,
                LastUpdateUserDepartment = alterBillView.LastUpdateUserDepartment,
                LastUpdateUser = alterBillView.LastUpdateUser,
                LastUpdateTime = alterBillView.LastUpdateTime
            };
            AlterBillRepository.Insert(record);
        }
        #endregion

        #region 保存单据和清单的过程数据
        public void SaveAlterBackup(AlterView alterView, List<AlterBillView> saveList)
        {
            AlterBackup record = AlterBackupRepository.Entities.FirstOrDefault(a => a.AlterID == alterView.AlterID && a.AssociatedNodeCode == alterView.AssociatedNodeCode && a.LastUpdateUserDepartment == alterView.LastUpdateUserDepartment && a.LastUpdateUser == alterView.LastUpdateUser);
            if (record != null)
            {
                UpdateAlterBackup(record, alterView);
            }
            else
            {
                InsertAlterBackup(alterView);
            }

            DeleteAlterBillBackup(alterView);
            foreach (AlterBillView alterBillView in saveList)
            {
                InsertAlterBillBackup(alterBillView);
            }
        }

        private void InsertAlterBackup(AlterView alterView)
        {
            AlterBackup record = new AlterBackup()
            {
                ID = alterView.ID,
                FlowCode = alterView.FlowCode,
                AlterID = alterView.AlterID,
                ProjectID = alterView.ProjectID,
                ContractID = alterView.ContractID,
                AlterCode = alterView.AlterCode,
                AlterName = alterView.AlterName,
                AlterPosition = alterView.AlterPosition,
                AlterType = alterView.AlterType,
                OriginalChartName = alterView.OriginalChartName,
                AlterChartName = alterView.AlterChartName,
                OriginalChartNo = alterView.OriginalChartNo,
                AlterChartNo = alterView.AlterChartNo,
                AlterReason = alterView.AlterReason,
                AlterMoney = alterView.AlterMoney,
                AlterDate = alterView.AlterDate,
                RequestPerson = alterView.RequestPerson,
                RequestDate = alterView.RequestDate,
                ProcedureMonitor = alterView.ProcedureMonitor,
                HandleState = alterView.HandleState,
                AssociatedNodeCode = alterView.AssociatedNodeCode,
                LastUpdateUserDepartment = alterView.LastUpdateUserDepartment,
                LastUpdateUser = alterView.LastUpdateUser,
                LastUpdateTime = alterView.LastUpdateTime,
                //SourceNodeCode = alterView.SourceNodeCode,
                //SourceUserDepartment = alterView.SourceUserDepartment,
                //SourceUser = alterView.SourceUser,
                SaveState = alterView.SaveState,
                SendState = alterView.SendState
            };
            AlterBackupRepository.Insert(record);
        }

        private void UpdateAlterBackup(AlterBackup record, AlterView alterView)
        {
            record.ProjectID = alterView.ProjectID;
            record.ContractID = alterView.ContractID;
            record.AlterCode = alterView.AlterCode;
            record.AlterName = alterView.AlterName;
            record.AlterPosition = alterView.AlterPosition;
            record.AlterType = alterView.AlterType;
            record.OriginalChartName = alterView.OriginalChartName;
            record.AlterChartName = alterView.AlterChartName;
            record.OriginalChartNo = alterView.OriginalChartNo;
            record.AlterChartNo = alterView.AlterChartNo;
            record.AlterReason = alterView.AlterReason;
            record.AlterMoney = alterView.AlterMoney;
            record.AlterDate = alterView.AlterDate;
            record.ProcedureMonitor = alterView.ProcedureMonitor;
            record.HandleState = alterView.HandleState;
            record.LastUpdateTime = alterView.LastUpdateTime;
            record.SaveState = alterView.SaveState;
            record.SendState = alterView.SendState;
            AlterBackupRepository.Update(record);
        }

        private void DeleteAlterBillBackup(AlterView alterView)
        {
            IQueryable<AlterBillBackup> lstAlterBillBackup = AlterBillBackupRepository.Entities.Where(a => a.PID == alterView.AlterID && a.AssociatedNodeCode == alterView.AssociatedNodeCode && a.LastUpdateUserDepartment == alterView.LastUpdateUserDepartment && a.LastUpdateUser == alterView.LastUpdateUser);
            List<Guid> lstDeleteID = new List<Guid>();
            foreach (AlterBillBackup alterBillBackup in lstAlterBillBackup)
            {
                lstDeleteID.Add(alterBillBackup.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                AlterBillBackupRepository.Delete(id);
            }
        }

        private void InsertAlterBillBackup(AlterBillView alterBillView)
        {
            AlterBillBackup record = new AlterBillBackup()
            {
                ID = alterBillView.ID,
                PID = alterBillView.PID,
                AlterBillID = alterBillView.AlterBillID,
                BillCode = alterBillView.BillCode,
                BillName = alterBillView.BillName,
                Unit = alterBillView.Unit,
                OriginalCount = alterBillView.OriginalCount,
                AfterAlterCount = alterBillView.AfterAlterCount,
                AlterCount = alterBillView.AlterCount,
                OriginalPrice = alterBillView.OriginalPrice,
                PresentPrice = alterBillView.PresentPrice,
                OriginalMoney = alterBillView.OriginalMoney,
                PresentMoney = alterBillView.PresentMoney,
                AlterMoney = alterBillView.AlterMoney,
                Remark = alterBillView.Remark,
                AssociatedNodeCode = alterBillView.AssociatedNodeCode,
                LastUpdateUserDepartment = alterBillView.LastUpdateUserDepartment,
                LastUpdateUser = alterBillView.LastUpdateUser,
                LastUpdateTime = alterBillView.LastUpdateTime,
                SendState = alterBillView.SendState
            };
            AlterBillBackupRepository.Insert(record);
        }
        #endregion

        #region 单据完成或者终止之后，删除所有sendState="0"的数据
        public void DeleteAlterProcessData(string alterID)
        {
            string sql = @"DELETE FROM AlterBackups WHERE AlterID='" + alterID + "' AND ISNULL(SendState,'0')='0';DELETE FROM AlterBillBackups WHERE PID='" + alterID + "' AND ISNULL(SendState,'0')='0';";
            AlterRepository.ExeSqlCommon(sql);
        }

        #endregion

        #region 撤回或者回退的时候处理单据的业务数据
        public void WithdrawOrBackAlterForRequestNode(string alterID, string nodeCode)
        {
            string sql = @"DELETE FROM Alters WHERE ID='" + alterID + "';" +
                          "DELETE FROM AlterBills WHERE PID='" + alterID + "';" +
                          "DELETE FROM AlterBackups WHERE AlterID='" + alterID + "' AND AssociatedNodeCode<>'" + nodeCode + "';" +
                          "DELETE FROM AlterBillBackups WHERE PID='" + alterID + "' AND AssociatedNodeCode<>'" + nodeCode + "';" +
                          "UPDATE AlterBackups SET HandleState='0',SaveState='1',SendState='0' WHERE AlterID='" + alterID + "';" +
                          "UPDATE AlterBillBackups SET SendState='0' WHERE PID='" + alterID + "';";
            AlterRepository.ExeSqlCommon(sql);
        }

        public void WithdrawOrBackAlterForOtherNode(string alterID, string nodeCode, string nodeCodeStr)
        {
            string sql = @"DELETE FROM AlterBills WHERE PID='" + alterID + "';" +
                          "DELETE FROM AlterBackups WHERE AlterID='" + alterID + "' AND AssociatedNodeCode IN (" + nodeCodeStr + ");" +
                          "DELETE FROM AlterBillBackups WHERE PID='" + alterID + "' AND AssociatedNodeCode IN (" + nodeCodeStr + ");" +
                          "UPDATE AlterBackups SET SendState='0' WHERE AlterID='" + alterID + "' AND AssociatedNodeCode='" + nodeCode + "';" +
                          "UPDATE AlterBillBackups SET SendState='0' WHERE PID='" + alterID + "'AND AssociatedNodeCode='" + nodeCode + "';";
            AlterRepository.ExeSqlCommon(sql);
        }
        #endregion

        #region 过程管理列表的界面数据展示和删除选中项
        public List<AlterView> GetAlterList()
        {
            string sql = "select * from V_ContractInfoes";
            var lstContractInfo = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);

            var queryList = from c in lstContractInfo
                            join a in AlterRepository.Entities
                            on c.ID equals a.ContractID
                            join p in ProjectInfoRepository.Entities
                            on a.ProjectID equals p.ID
                            select new
                            {
                                a.ID,
                                a.FlowCode,
                                a.ProjectID,
                                p.ProjectName,
                                a.ContractID,
                                c.ContractName,
                                a.AlterCode,
                                a.AlterName,
                                a.AlterPosition,
                                a.AlterType,
                                a.OriginalChartName,
                                a.AlterChartName,
                                a.OriginalChartNo,
                                a.AlterChartNo,
                                a.AlterReason,
                                a.AlterMoney,
                                a.AlterDate,
                                a.RequestPerson,
                                a.RequestDate,
                                a.ProcedureMonitor,
                                a.HandleState,
                                a.LastUpdateUserDepartment,
                                a.LastUpdateUser,
                                a.LastUpdateTime,
                                c.LevelCode
                            };

            List<AlterView> alterViewList = new List<AlterView>();
            foreach (var query in queryList)
            {
                alterViewList.Add(new AlterView()
                {
                    ID = GuidHelper.NewGuid(),
                    AlterID = query.ID,
                    FlowCode = query.FlowCode,
                    ProjectID = query.ProjectID,
                    ProjectName = query.ProjectName,
                    ContractID = query.ContractID,
                    ContractName = query.ContractName,
                    AlterCode = query.AlterCode,
                    AlterName = query.AlterName,
                    AlterPosition = query.AlterPosition,
                    AlterType = query.AlterType,
                    OriginalChartName = query.OriginalChartName,
                    AlterChartName = query.AlterChartName,
                    OriginalChartNo = query.OriginalChartNo,
                    AlterChartNo = query.AlterChartNo,
                    AlterReason = query.AlterReason,
                    AlterMoney = query.AlterMoney,
                    AlterDate = query.AlterDate,
                    RequestPerson = query.RequestPerson,
                    RequestDate = query.RequestDate,
                    ProcedureMonitor = query.ProcedureMonitor,
                    HandleState = query.HandleState,
                    AssociatedNodeCode = "",
                    LastUpdateUserDepartment = query.LastUpdateUserDepartment,
                    LastUpdateUser = query.LastUpdateUser,
                    LastUpdateTime = query.LastUpdateTime,
                    LevelCode = query.LevelCode
                });
            }

            var queryProcessList = from c in lstContractInfo
                                   join a in AlterBackupRepository.Entities
                                   on c.ID equals a.ContractID
                                   join p in ProjectInfoRepository.Entities
                                   on a.ProjectID equals p.ID
                                   where a.HandleState == "0"
                                   select new
                                   {
                                       a.AlterID,
                                       a.FlowCode,
                                       a.ProjectID,
                                       p.ProjectName,
                                       a.ContractID,
                                       c.ContractName,
                                       a.AlterCode,
                                       a.AlterName,
                                       a.AlterPosition,
                                       a.AlterType,
                                       a.OriginalChartName,
                                       a.AlterChartName,
                                       a.OriginalChartNo,
                                       a.AlterChartNo,
                                       a.AlterReason,
                                       a.AlterMoney,
                                       a.AlterDate,
                                       a.RequestPerson,
                                       a.RequestDate,
                                       a.ProcedureMonitor,
                                       a.HandleState,
                                       a.LastUpdateUserDepartment,
                                       a.LastUpdateUser,
                                       a.LastUpdateTime,
                                       c.LevelCode
                                   };

            foreach (var query in queryProcessList)
            {
                alterViewList.Add(new AlterView()
                {
                    ID = GuidHelper.NewGuid(),
                    AlterID = query.AlterID,
                    FlowCode = query.FlowCode,
                    ProjectID = query.ProjectID,
                    ProjectName = query.ProjectName,
                    ContractID = query.ContractID,
                    ContractName = query.ContractName,
                    AlterCode = query.AlterCode,
                    AlterName = query.AlterName,
                    AlterPosition = query.AlterPosition,
                    AlterType = query.AlterType,
                    OriginalChartName = query.OriginalChartName,
                    AlterChartName = query.AlterChartName,
                    OriginalChartNo = query.OriginalChartNo,
                    AlterChartNo = query.AlterChartNo,
                    AlterReason = query.AlterReason,
                    AlterMoney = query.AlterMoney,
                    AlterDate = query.AlterDate,
                    RequestPerson = query.RequestPerson,
                    RequestDate = query.RequestDate,
                    ProcedureMonitor = query.ProcedureMonitor,
                    HandleState = query.HandleState,
                    AssociatedNodeCode = "",
                    LastUpdateUserDepartment = query.LastUpdateUserDepartment,
                    LastUpdateUser = query.LastUpdateUser,
                    LastUpdateTime = query.LastUpdateTime,
                    LevelCode = query.LevelCode
                });
            }

            return alterViewList.OrderBy(a => a.ProjectName).ThenBy(a => a.LevelCode).ToList();
        }

        public OperationResult DeleteSelectAlter(string alterID)
        {
            try
            {
                PublicHelper.CheckArgument(alterID, "id");
                Guid guidID = Guid.Parse(alterID);
                Alter record = AlterRepository.Entities.FirstOrDefault(m => m.ID == guidID);
                AlterBackup recordBackup = AlterBackupRepository.Entities.FirstOrDefault(m => m.AlterID == guidID);
                if (record == null && recordBackup == null)
                    return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");
                if (record != null)
                {
                    AlterRepository.Delete(record);
                }

                string sql = @"DELETE FROM AlterBills WHERE PID='" + alterID + "';" +
                              "DELETE FROM AlterBackups WHERE AlterID='" + alterID + "';" +
                              "DELETE FROM AlterBillBackups WHERE PID='" + alterID + "';" +
                              "DELETE FROM BaklogTasks WHERE RecordID='" + alterID + "';" +
                              "DELETE FROM BaklogTaskTemps WHERE RecordID='" + alterID + "';" +
                              "DELETE FROM CompleteFlowNodes WHERE RecordID='" + alterID + "';" +
                              "DELETE FROM FlowGraphNodes WHERE RecordID='" + alterID + "';" +
                              "DELETE FROM FlowProcessMessages WHERE RecordID='" + alterID + "';" +
                              "DELETE FROM ProcessedTasks WHERE RecordID='" + alterID + "';";
                AlterRepository.ExeSqlCommon(sql);
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, "删除失败");
            }
        }
        #endregion

        #region 选择流转完成的变更申请，用在变更令选变更申请的地方
        public List<AlterView> GetFinishAlter(Guid projectID, Guid contractID)
        {
            List<AlterView> lstAlterView = new List<AlterView>();
            IQueryable<Alter> lstAlter = AlterRepository.Entities.Where(a => a.ProjectID == projectID && a.ContractID == contractID && a.HandleState == "2");
            foreach (Alter alter in lstAlter)
            {
                lstAlterView.Add(new AlterView()
                {
                    ID = alter.ID,
                    AlterCode = alter.AlterCode,
                    AlterName = alter.AlterName
                });
            }
            return lstAlterView;
        }

        public List<AlterBillView> GetAlterBillListByAlterIDStr(string alterIDStr, Guid pid)
        {
            string[] alterIDArr = alterIDStr.Split(',');
            alterIDStr = string.Empty;
            foreach (string alterID in alterIDArr)
            {
                alterIDStr += "'" + alterID + "',";
            }
            string sql = @" SELECT  newid() as ID
                                    ,newid()  as PID
                                    ,BillCode
		                            ,BillName
		                            ,Unit
		                            ,OriginalCount
		                            ,OriginalCount+SUM(AlterCount) as AfterAlterCount
		                            ,SUM(AlterCount) as AlterCount
		                            ,OriginalPrice
		                            ,SUM(AlterMoney)/SUM(AlterCount) as PresentPrice
		                            ,OriginalMoney
		                            ,OriginalMoney+SUM(AlterMoney) as PresentMoney
		                            ,SUM(AlterMoney) as AlterMoney
		                            ,Remark 
		                            ,'1' as LastUpdateUserDepartment 
		                            ,'1' as LastUpdateUser  
		                            ,Convert(datetime,'2014-2-26') as LastUpdateTime  
		                            ,CONVERT(bit,0)  as  IsDeleted
                                    ,Convert(datetime,'2014-2-26') as AddDate
                            FROM AlterBills WHERE PID IN (" + alterIDStr.TrimEnd(',') + @")
                            GROUP BY BillCode,BillName,Unit,OriginalCount,OriginalPrice,OriginalMoney,Remark;";
            IEnumerable<AlterBill> alterBills = AlterBillRepository.SqlQueryCommon(sql);
            List<AlterBillView> alterBillViewList = new List<AlterBillView>();
            foreach (AlterBill alterBill in alterBills)
            {
                alterBillViewList.Add(new AlterBillView()
                {
                    PID = pid,
                    BillCode = alterBill.BillCode,
                    BillName = alterBill.BillName,
                    Unit = alterBill.Unit,
                    OriginalCount = alterBill.OriginalCount,
                    AfterAlterCount = alterBill.AfterAlterCount,
                    AlterCount = alterBill.AlterCount,
                    OriginalPrice = alterBill.OriginalPrice,
                    PresentPrice = alterBill.PresentPrice,
                    OriginalMoney = alterBill.OriginalMoney,
                    PresentMoney = alterBill.PresentMoney,
                    AlterMoney = alterBill.AlterMoney,
                    Remark = alterBill.Remark
                });
            }
            return alterBillViewList;
        }
        #endregion
    }
}